package cn.hzu580.service.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.opensymphony.xwork2.ActionContext;

import cn.hzu580.bean.NoticeSender;
import cn.hzu580.dao.BaseDAO;
import cn.hzu580.entity.Message;
import cn.hzu580.entity.Mission;
import cn.hzu580.entity.User;
import cn.hzu580.service.MessageService;
import cn.hzu580.util.DateUtil;
import cn.hzu580.vo.MessageVO;

@Service("message")
public class MessageServiceImpl implements MessageService {

	@Resource(name="DAO")
	private BaseDAO baseDAO;
	
	@Resource
	private NoticeSender sender;
	
	@Override
	public void write(MessageVO vo) {
		//获取该mission的所有message，判断回复对象在不在内
		Map session = ActionContext.getContext().getSession();
		User user = (User) baseDAO.get(User.class, (Serializable) session.get("uid"));
		Mission mission = (Mission) baseDAO.get(Mission.class, vo.getMission().getId());
		String message_object = vo.getObject().getUid();
		String mission_owner = mission.getOwner().getUid();
		List<Message> list = getAll(vo);
		boolean objectIsExist = false;
		if(list != null){
			for (Message m : list) {
				String message_owner = m.getOwner().getUid();
				if(message_owner.equals(message_object)){
					objectIsExist = true;
					break;
				}
			}
		}
		if(mission_owner.equals(message_object)){
			objectIsExist = true;
		}
		if(objectIsExist){
			//判断留言的任务有没有被删除
			if(mission.getState() == 5 || mission.getState() == -2){
				throw new RuntimeException("mission has been deleted");
			}
			Message message = new Message();
			message.setTime(DateUtil.now());
			message.setOwner(user);
			message.setMission(vo.getMission());
			message.setObject(vo.getObject());
			message.setContent(vo.getContent());
			baseDAO.insert(message);
			//如果接受和发送都为同一uid则无需通知
			if(!user.getUid().equals(message_object)){
				sender.send(user.getName()+"回复了你消息", vo.getObject(), vo.getMission());
			}
			return;
		}
		throw new RuntimeException("object is not in the list of message for this mission");
	}

	
	@Override
	public void delete(MessageVO vo) {
		Message message = (Message) baseDAO.get(Message.class, vo.getId());
		//判断留言是否是自己的
		if(!ActionContext.getContext().getSession().get("uid").equals(message.getOwner().getUid())){
			throw new RuntimeException("message's owner is not you");
		}
		message.setHas_delete(true);
		baseDAO.update(message);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Message> getAll(MessageVO vo) {
		Map<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("mission_id", vo.getMission().getId());
		List<Message> list = (List<Message>) baseDAO.exeQuery("from Message where mission.id = :mission_id and has_delete = false", parameters);
		return list;
	}

	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	public NoticeSender getSender() {
		return sender;
	}

	public void setSender(NoticeSender sender) {
		this.sender = sender;
	}

	
	
	
	
}
